/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = mysql_dissect_greeting.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <mysql/mysql_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/mysql/config.h"

extern int mysql_dissect_greeting(L7session_mysql_private *p_st_private);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));

    i_ret = mysql_dissect_greeting(NULL);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    L7_fragment_mysql st_fragment;
    char *p_data = NULL;
    p_data = (char *)malloc(10);
    memset(p_data, 0x00, 10);
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 4;
    memcpy(p_data, (char *)"\xff\x00\x00\x02", 4);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 4);
    
    st_mysql.p_st_fragment = &st_fragment;

    i_ret = mysql_dissect_greeting(&st_mysql);
    
    bytebuff_free(&st_fragment.p_st_data);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    
    free(p_data);
    
    return;
}

static void case3(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    L7_fragment_mysql st_fragment;
    char *p_data = NULL;
    p_data = (char *)malloc(10);
    memset(p_data, 0x00, 10);
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 4;
    memcpy(p_data, (char *)"\x05\x00\x00\x02", 4);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 4);
    
    st_mysql.p_st_fragment = &st_fragment;

    i_ret = mysql_dissect_greeting(&st_mysql);
    
    bytebuff_free(&st_fragment.p_st_data);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    
    free(p_data);
    
    return;
}

int add_suite_mysql_dissect_greeting(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" mysql_dissect_greeting()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
